{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ipympl: The Matplotlib Jupyter Widget Backend\n",
    "\n",
    "## https://github.com/matplotlib/ipympl\n",
    "\n",
    "\n",
    "Enabling interaction with matplotlib charts in the Jupyter notebook and JupyterLab\n",
    "\n",
    "- BSD-3-Clause\n",
    "\n",
    "**Installation:**\n",
    "\n",
    "```bash\n",
    "conda install -c conda-forge ipympl\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Enabling the `widget` backend. This requires ipympl. ipympl can be install via pip or conda."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import VBox, FloatSlider"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When using the `widget` backend from ipympl, fig.canvas is a proper Jupyter interactive widget, which can be embedded in Layout classes like HBox and Vbox.\n",
    "\n",
    "One can bound figure attributes to other widget values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.ioff()\n",
    "plt.clf()\n",
    "\n",
    "slider = FloatSlider(\n",
    "    value=1.0,\n",
    "    min=0.02,\n",
    "    max=2.0\n",
    ")\n",
    "\n",
    "fig1 = plt.figure(1)\n",
    "\n",
    "x1 = np.linspace(0, 20, 500)\n",
    "\n",
    "lines = plt.plot(x1, np.sin(slider.value  * x1))\n",
    "\n",
    "def update_lines(change):\n",
    "    lines[0].set_data(x1, np.sin(change.new * x1))\n",
    "    fig1.canvas.draw()\n",
    "    fig1.canvas.flush_events()\n",
    "\n",
    "slider.observe(update_lines, names='value')\n",
    "\n",
    "VBox([slider, fig1.canvas])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mpl_toolkits.mplot3d import axes3d\n",
    "\n",
    "fig2 = plt.figure(2)\n",
    "ax = fig2.add_subplot(111, projection='3d')\n",
    "\n",
    "# Grab some test data.\n",
    "X, Y, Z = axes3d.get_test_data(0.05)\n",
    "\n",
    "# Plot a basic wireframe.\n",
    "ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)\n",
    "\n",
    "fig2.canvas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "np.random.seed(0)\n",
    "\n",
    "n_bins = 10\n",
    "x2 = np.random.randn(1000, 3)\n",
    "\n",
    "fig3, axes = plt.subplots(nrows=2, ncols=2)\n",
    "ax0, ax1, ax2, ax3 = axes.flatten()\n",
    "\n",
    "colors = ['red', 'tan', 'lime']\n",
    "ax0.hist(x2, n_bins, density=1, histtype='bar', color=colors, label=colors)\n",
    "ax0.legend(prop={'size': 10})\n",
    "ax0.set_title('bars with legend')\n",
    "\n",
    "ax1.hist(x2, n_bins, density=1, histtype='bar', stacked=True)\n",
    "ax1.set_title('stacked bar')\n",
    "\n",
    "ax2.hist(x2, n_bins, histtype='step', stacked=True, fill=False)\n",
    "ax2.set_title('stack step (unfilled)')\n",
    "\n",
    "# Make a multiple-histogram of data-sets with different length.\n",
    "x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]\n",
    "ax3.hist(x_multi, n_bins, histtype='bar')\n",
    "ax3.set_title('different sample sizes')\n",
    "\n",
    "fig3.tight_layout()\n",
    "fig3.canvas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "widgets-tutorial",
   "language": "python",
   "name": "widgets-tutorial"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
